package no.testspill;

import java.util.ArrayList;

import no.logic.Collision;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.view.View;

public class Ball2 extends View{
	
	MediaPlayer sound;
	private float posX ,posY;
	private float dX, dY;
	private double velocity;
	private Bitmap image;
	private float maxHeight = 0;
	private float maxWidth = 0;
	private final float ACCFAC = 0.03f;
	

	public Ball2(Context context, float height, float width) {
		super(context);
		maxHeight = height;
		maxWidth = width;
		posX = (width/2);
		posY = height/2;
		velocity = 10;
		setRandomDirecion();
		image = BitmapFactory.decodeResource(getResources(), R.drawable.purple_ball);
	}
	
	public void setMaxHeight(float h){
		maxHeight = h;
	}
	
	public void setMaxWidth(float h){
		maxWidth = h;
	}
	
	public float getMaxHeight(){
		return maxHeight;
	}
	
	public float getMaxWidth(){
		return maxWidth;
	}
	
	public float getPosX(){
		return posX;
	}
	
	public float getPosY(){
		return posY;
	}
	
	public void setPosX(float x){
		posX = x;
	}
	
	public void setPosY(float y){
		posY = y;
	}
	
	public double getVelocity(){
		return velocity;
	}
	
	public float getDx(){
		return dX;
	}
	
	public float  getDy(){
		return dY;
	}
	
	public void setDx(float dX){
		this.dX = dX;
	}
	
	public void setDy(float dY){
		this.dY = dY;
	}
	
	public Bitmap getImage(){
		return image;
	}
	
	
	
	public void update(Canvas c, ArrayList<LineSegment> lines, Bitmap background){
		dX = (posX > (c.getWidth()-image.getWidth()) || posX<0 )?dX*(-1):dX;
		dY = (posY > (c.getHeight()-(image.getHeight())-5) || posY<0 )?dY*(-1):dY;
		posX+=dX;
		posY+=dY;
		velocity = (velocity-ACCFAC <= 0)? 0:velocity-ACCFAC; 
		normalize();
		
//		if(background.getPixel((int)getPosX(), (int)getPosY())!=-1){
//			setDx(0);
//			setDy(0);
//		}
//		if(background.getPixel((int)getPosX(), (int)getPosY())==-4856040){
//			Paint paint = new Paint();
//			paint.setColor(Color.BLUE);
//			setDx(0);
//			setDy(0);
//			c.drawText("DU VANT!!", 220, 390, paint);
//		}
		
		
		if(!lines.isEmpty()){
			for (int i = 0; i < lines.size(); i++) {
				if(lines.get(i).checkCollision(posX, posY)){
					Collision.reflect(this, lines.get(i));
//					this.setDx(0);
//					this.setDy(0);
//					lines.get(i).printCoordinates();
//					lines.get(i).drawTestLine(c);
				}
			} 
		}
		c.drawBitmap(image, posX, posY, null);
	}
	
	public void setRandomDirecion(){
		dX = (int) (Math.random()*4) + 1;
		dY = (int) (Math.random()*4) + 1;
		normalize(); 
//		
//		System.out.println("SetRandomDirection");
//		System.out.println("X: "+ dX);
//		System.out.println("Y: "+ dY);
	}
	
	public void normalize() {
		double vel = (double) Math.sqrt(Math.pow(dX, 2)+Math.pow(dY, 2));
		dX /=vel;
		dY /=vel;
		dX *= velocity;
		dY *= velocity;
//		System.out.println("Normalize:");
//		System.out.println(velocity);
//		System.out.println("X: "+ dX);
//		System.out.println("Y: "+ dY);
		
	}
	
}
